package 模拟;

import java.util.Arrays;

public class 替换所有的问号1 {
    //https://leetcode.cn/problems/replace-all-s-to-avoid-consecutive-repeating-characters/description/
    public String modifyString1(String s) {
        char[] array = s.toCharArray();
        for (int i = 0; i < array.length; i++) {
            if (array[i] == '?') {
                char tmp1 = 0, tmp2 = 0;
                if (i - 1 >= 0) {
                    tmp1 = array[i - 1];
                }
                if (i + 1 < array.length) {
                    tmp2 = array[i + 1];
                }
                for (char j = 'a'; j <= 'z'; j++) {
                    if (j != tmp1 && j != tmp2) {
                        array[i] = j;
                        break;
                    }
                }
            }
        }
//        return Arrays.toString(array);
//        return array.toString(); // Object对象的toString，返回的是该对象的地址
        return String.valueOf(array);
    }

    public String modifyString2(String s) {
        int a = 'a';
        char b = 1;
        char[] array = s.toCharArray();
        for (int i = 0; i < array.length; i++) {
            if (array[i] == '?') {
                char tmp1 = 0, tmp2 = 0;
                if (i - 1 >= 0) {
                    tmp1 = array[i - 1];
                }
                if (i + 1 < array.length) {
                    tmp2 = array[i + 1];
                }
                for (int j = 'a'; j <= 'z'; j++) {
                    if (j != tmp1 && j != tmp2) {
                        //array[i] = j; // 为什么char b 可以用int赋值，而循环里的同样是char类型的array[i]不能用int赋值？编译器这里没能确定int转char是否会丢数值(虽然在循环里没超的)
                        break;
                    }
                }
            }
        }
//        return Arrays.toString(array);
//        return array.toString(); // Object对象的toString，返回的是该对象的地址
        return String.valueOf(array);
    }

    public String modifyString(String ss) {
        char[] s = ss.toCharArray();
        int n = s.length;
        for (int i = 0; i < n; i++) {
            if(s[i] == '?') {
                for (char ch = 'a'; ch <= 'z'; ch++) {
                    if((i == 0 || ch != s[i - 1]) && (i == n - 1 || ch != s[i + 1])) {
                        s[i] = ch;
                        break;
                    }
                }
            }
        }
        return String.valueOf(s);
    }
}
